﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.Common;

namespace QLHPHP
{
    /// <summary>
    /// Class ve cac khoa
    /// </summary>
    public static class Khoa
    {
        /// <summary>
        /// Lay danh sach cac khoa cua truong
        /// </summary>
        /// <returns></returns>
        public static DataTable LayDsKhoa()
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "GetDepartment";
            return DataAccess.ExecuteSelectCommand(comm);
        }

        /// <summary>
        /// Cap nhat danh sach cac khoa trong truong
        /// </summary>
        /// <param name="TenKhoa">Ten khoa</param>
        /// <param name="MaKhoa">Ma khoa</param>
        /// <returns></returns>
        public static bool CapNhatKhoa(string TenKhoa, string MaKhoa)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "UpdateDepartment";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@TenKhoa";
            param.DbType = DbType.String;
            param.Value = TenKhoa;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@MaKhoa";
            param.DbType = DbType.String;
            param.Value = MaKhoa;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Them 1 khoa vao danh sach khoa
        /// </summary>
        /// <param name="TenKhoa">Ten khoa can them vao</param>
        /// <param name="MaKhoa">Ma khoa</param>
        /// <returns></returns>
        public static bool ThemKhoa(string TenKhoa, string MaKhoa)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "CreateDepartment";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@TenKhoa";
            param.DbType = DbType.String;
            param.Value = TenKhoa;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@MaKhoa";
            param.DbType = DbType.String;
            param.Value = MaKhoa;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }


        /// <summary>
        /// Xoa khoa khoi danh sach khoa
        /// </summary>
        /// <param name="MaKhoa">Ma khoa can xoa</param>
        /// <returns></returns>
        public static bool XoaKhoa(string MaKhoa)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "DeleteDepartment";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaKhoa";
            param.DbType = DbType.String;
            param.Value = MaKhoa;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Kiểm tra xem khoa có chứa ngành nào không, nếu chưa thì cho phép xóa, nếu có rồi thì không cho phép xóa
        /// </summary>
        /// <param name="MaKhoa">Mã khoa</param>
        /// <returns></returns>
        public static bool KTKhoaCoChuaNganh(string MaKhoa)
        {
            DbCommand comm = DataAccess.CreateCommand();
            comm.CommandText = "CheckDepartmentContainsBranch";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@MaKhoa";
            param.DbType = DbType.String;
            param.Value = MaKhoa;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@CoChua";
            param.DbType = DbType.Boolean;
            param.Direction = ParameterDirection.Output;
            comm.Parameters.Add(param);

            int result = DataAccess.ExecuteNonQuery(comm);

            bool check = (bool)comm.Parameters[1].Value;
            return check;
        }
    }
}
